﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;

namespace ReportingCenterSite
{
    public partial class home : System.Web.UI.Page
    {
        List<Team> teams = null;
   
        protected override void OnPreInit(EventArgs e)
        {
            ReportingCenterContentContainer db = new ReportingCenterContentContainer();
            var t = from team in db.TeamSet
                    select team;
            teams = t.ToList();
            base.OnPreInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                header0.Text = teams[0].ShortTeamName;
                header1.Text = teams[1].ShortTeamName;
                team_intro0.Text = teams[0].TeamIntroduction;
                reports_category_list0.Text = GenerateTopReportsNavList(0);
                top_reports0.Text = GenerateTopReports(0);
                misc0.Text = GenerateMiscList(0, 5);
                issue0.Text = GenerateIssueList(0, 5);
                news0.Text = GenerateNewsList(0, 5);
                team_members0.Text = GenerateTeamMembers(0);
            }
        }

        protected void trigger_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                int k = Convert.ToInt32(counter.Text);
                int index = Convert.ToInt32(teamIndex.Text);
                int curInnerIndex = (k + 1) % 2;

                //Update header
                Label prevHeader = (Label)FindControl(string.Format("header{0}", (k) % 3));
                prevHeader.Text = teams[(index + 2) % teams.Count].ShortTeamName;

                Label currentHeader = (Label)FindControl(string.Format("header{0}", (k+1) % 3));
                currentHeader.Text = teams[index].ShortTeamName;

                Label nextHeader = (Label)FindControl(string.Format("header{0}", (k+2) % 3));
                nextHeader.Text = teams[(index + 1) % teams.Count].ShortTeamName;

                //Update body
                Literal curTeamIntro = (Literal)FindControl(string.Format("team_intro{0}", curInnerIndex));
                curTeamIntro.Text = teams[index].TeamIntroduction;
                
                //Update top reports navigation
                Literal curReportsCatogoryList = (Literal)FindControl(string.Format("reports_category_list{0}", curInnerIndex));
                curReportsCatogoryList.Text = GenerateTopReportsNavList(index);
                
                //Update top reports
                Literal curReports = (Literal)FindControl(string.Format("top_reports{0}", curInnerIndex));
                curReports.Text = GenerateTopReports(index);

                //Update misc
                Literal curMisc = (Literal)FindControl(string.Format("misc{0}", curInnerIndex));
                curMisc.Text = GenerateMiscList(index, 5);

                //Update issue
                Literal curIssue = (Literal)FindControl(string.Format("issue{0}", curInnerIndex));
                curIssue.Text = GenerateIssueList(index, 5);

                //Update news
                Literal curNews = (Literal)FindControl(string.Format("news{0}", curInnerIndex));
                curNews.Text = GenerateNewsList(index, 5);

                //Update team members
                Literal curTeamMembers = (Literal)FindControl(string.Format("team_members{0}", curInnerIndex));
                curTeamMembers.Text = GenerateTeamMembers(index);
            }
        }

        protected void trigger2_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                int k = Convert.ToInt32(counter.Text);
                int index = Convert.ToInt32(teamIndex.Text);
                int curInnerIndex = (k + 1) % 2;
                //Get dashboard
                GetDashboard(index, curInnerIndex);
            }
        }

        protected void trigger3_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                int index = Convert.ToInt32(teamIndex.Text);
                int id = Convert.ToInt32(newsID.Text);
                string content = "";
                switch(newsType.Text)
                {
                    case "misc":
                        content = (from c in teams[index].Misc
                                   where c.Id == id
                                   select c.Content).First();                                  
                        break;
                    case "news":
                        content = (from c in teams[index].News
                                   where c.Id == id
                                   select c.Content).First();
                        break;
                    case "issue":
                        content = (from c in teams[index].Issue
                                   where c.Id == id
                                   select c.Content).First();
                        break;
                }
                dialogContent.Text = content;                               
            }
        }

        //Get current dashboard
        protected void GetDashboard(int index, int k)
        {
            ReportViewer rv = (ReportViewer)FindControl(string.Format("ReportViewer{0}", k));
            rv.Reset();
            if (teams[index].Dashboard.Count > 0)
            {
                rv.ServerReport.ReportServerUrl = new Uri(teams[index].Dashboard.ElementAt(0).ReportServer);
                rv.ServerReport.ReportPath = teams[index].Dashboard.ElementAt(0).ReportPath;
            }
        }

        //Generate team members
        //HTML reference
        /*
        <li>
            <a id="a1">
                <img src="styles/images/head.png" alt=""/>
                <p>
                    <strong>Freebies</strong><br />
                    Download free files to make your job easier.
                </p>
            </a>
        </li>
        */
        protected string GenerateTeamMembers(int index)
        {
            string res = "";

            //DrWhom.PeopleStore ps = new DrWhom.PeopleStore();
            //ps.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            //DrWhom.Person person = null;       
            
            for (int i = 0; i < teams[index].People.Count; ++i)
            {
                try
                {
                    //person = ps.FindPersonByAlias(teams[index].People.ElementAt(i).Alias);
                    //if (person == null)
                    //{
                    //    break;
                    //}
                    res += string.Format(
                        "<li><a id=\"a{0}\"><div id=\"crop\"><img src=\"http://who/Photos/{1}.jpg\" alt=\"\"/><p><strong>{2}</strong><br />{3}</p></div></a></li>",
                        i,
                        teams[index].People.ElementAt(i).Alias,
                        teams[index].People.ElementAt(i).Alias,
                        string.Format("{0}@microsoft.com", teams[index].People.ElementAt(i).Alias)
                        );
                }
                catch { }
            }
            return res;
        }

        //Generate misc list
        //HTML reference
        //<li><a href="#">Misc sample record for test</a></li>
        protected string GenerateMiscList(int index, int count)
        {
            string res = "";
            for (int i = 0; (i < count && i < teams[index].Misc.Count); ++i)
            {
                res += string.Format(
                    "<li><a href=\"{1}\" class=\"misc\">{0}</a></li>",
                    teams[index].Misc.ElementAt(i).Title,
                    teams[index].Misc.ElementAt(i).Id
                    );
            }
            return res;
        }

        protected string GenerateIssueList(int index, int count)
        {
            string res = "";
            for (int i = 0; (i < count && i < teams[index].Issue.Count); ++i)
            {
                res += string.Format(
                    "<li><a href=\"{1}\" class=\"issue\">{0}</a></li>",
                    teams[index].Issue.ElementAt(i).Title,
                    teams[index].Issue.ElementAt(i).Id
                    );
            }
            return res;
        }

        protected string GenerateNewsList(int index, int count)
        {
            string res = "";
            for (int i = 0; (i < count && i < teams[index].News.Count); ++i)
            {
                res += string.Format(
                    "<li><a href=\"{1}\" class=\"news\">{0}</a></li>",
                    teams[index].News.ElementAt(i).Title,
                    teams[index].News.ElementAt(i).Id
                    );
            }
            return res;
        }

        //Generate navigation in top reports
        protected string GenerateTopReportsNavList(int index)
        {
            string res = "";
            int i = 0;
            foreach (var c in teams[index].ReportCategory)
            {
                ++i;
                res += String.Format("<li><a href='#{0}'>{1}</a></li>", i, c.ReportCategoryName);
            }
            return res.Insert(3, " class=\"first\"");
        }

        //Generate top reports
        //HTML reference
        /*
            <div>
                <div class="report first">
                    <img src="styles/images/report_icon_1.png" alt />
                    <div>
                        <h1>Sample Report</h1>
                        This is a sample report introduction. 
                        <a href="#">Open</a>
                    </div>
                </div>
            </div>         
         */
        protected string GenerateTopReports(int index)
        {
            string res = "";
            for (int i = 0; i < teams[index].ReportCategory.Count; ++i)
            {
                string subRes = "";
                var reports = (from r in teams[index].ReportCategory.ElementAt(i).Report 
                               orderby r.Id descending
                               select r
                               ).Take(3);

                int j = 0;
                foreach (var r in reports)
                {
                    subRes += string.Format(
                        "<div class=\"report\"><img src=\"styles/images/report_icon_{0}.png\" alt=\"\" /><div><h1>{1}</h1>{2}<a href=\"{3}\" target=\"_blank\">Open</a></div></div>",
                        j+1,
                        r.ReportName,
                        r.ReportDescription,
                        r.ReportURL
                        );
                    ++j;
                }
                if (subRes.Length > 0)
                {
                    subRes = subRes.Insert(subRes.IndexOf("report"), "first ");
                }
                res += "<div>"+subRes+"</div>";
            }


            return res;
        }
      
        //To generate navigation list
        protected string GenerateNavList()
        {
            string res = "";
            foreach (Team t in teams)
            {
                res += String.Format("<li>{0}</li>", t.ShortTeamName);
            }

            return res.Insert(3, " class=\"first\"");
        }
    }
}